<template>
	<view class="done-page">
		<!-- 页面头部 -->
		<view class="page-header">
			<view class="header-content">
				<text class="page-title">🏆 已完成任务</text>
				<text class="task-count">{{ cpatrolList.length }} 个任务</text>
			</view>
		</view>

		<!-- 空状态 -->
		<view v-if="!isLoading && cpatrolList.length === 0" class="empty-state">
			<text class="empty-icon">🏆</text>
			<text class="empty-title">暂无已完成任务</text>
			<text class="empty-desc">您完成的巡查任务将显示在这里</text>
		</view>

		<!-- 任务列表 -->
		<view v-else class="task-list">
			<view 
				v-for="(item, index) in cpatrolList" 
				:key="item.pid || index" 
				class="task-card"
				@click="updateCpatrol(item)"
			>
				<!-- 卡片头部 -->
				<view class="card-header">
					<view class="task-time">
						<text class="time-icon">⏰</text>
						<text class="time-text">{{ formatTime(item.inserttime) }}</text>
					</view>
					<StatusBadge 
						:text="item.presult || '未知'"
						:type="getResultType(item.presult)"
					/>
				</view>

				<!-- 项目名称 -->
				<view class="project-name">
					<text class="name-text">{{ item.cproject || '未知项目' }}</text>
				</view>

				<!-- 任务信息 -->
				<view class="task-info">
					<view class="info-row">
						<text class="info-label">📝 单号</text>
						<text class="info-value">{{ item.pid || '--' }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">📋 类型</text>
						<text class="info-value">{{ item.ptype || '--' }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">🔍 模式</text>
						<text class="info-value">{{ item.pmode || '--' }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">👤 巡查人</text>
						<text class="info-value">{{ item.pman1 || '--' }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">⏰ 巡查时间</text>
						<text class="info-value">{{ formatDateTime(item.patroltime) }}</text>
					</view>
					<view class="info-row">
						<text class="info-label">📊 流程节点</text>
						<text class="info-value flow-node">{{ getFlowNodeName(item.rf_flow_node) }}</text>
					</view>
					<view v-if="item.totalscore" class="info-row score-row">
						<text class="info-label">🏅 总得分</text>
						<text class="score-value">{{ item.totalscore }}分</text>
					</view>
				</view>

				<!-- 完成标记 -->
				<view class="completion-badge">
					<text class="completion-icon">✓</text>
					<text class="completion-text">已完成</text>
				</view>
			</view>
		</view>

		<!-- 加载状态 -->
		<LoadingSpinner v-if="isLoading" :overlay="true" text="加载中..." />
	</view>
</template>

<script>
	import {
		request
	} from '@/request/index.js';
	export default {
		data() {
			return {
				cpatrolList: [],
				showFilter:false,
				
				
				
				pmode: '',
				pModes: [],
				index: 0,
				presult: '',
				presults: [],
				presultindex: 0,
				cproject:'',
				cprojects:[],
				cprojectIndex:0,
				
				showreform:false,
				showapproved2:false,
				showreject2:false,
				showreject2:false,
				showuploadReform:false,
				showreview1Pass:false,
				showreview1reject:false,
				showreview2Pass:false,
				showreview2reject:false,
				userInfo:{}
			}
		},
		show(){
			this.getCpatrol()
		},
		onLoad() {
			var userInfo = uni.getStorageSync('user');
			if (userInfo == null || userInfo == 'null' || userInfo == '') {
				uni.showToast({
					title: "请先登录",
					success() {
						uni.reLaunch({
							url: "/pages/login/login"
						})
					}
				})
			
			}
			
			var _self = this;
			_self.userInfo = userInfo
			
			this.getCpatrol()
			this.getPmode()
			this.getPresults()
			this.getProjects()
		},
		onPullDownRefresh(){
			this.getCpatrol()
		},
		methods: {
			goReform(pid){
				uni.redirectTo({
					url:"/pages/reform/reform?pid="+pid
				})
			},
			//提交限期整改
			reform(pid){
				var data ={}
				data = {pids:pid}
				request({
					url: "/patrol2/limitRectifySubmit",
					method: "POST",
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					if (res.data.success == true) {
						uni.showToast({
							title: '操作成功',
							
						})
						this.getCpatrol()
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			//二级审核通过
			approved2(pid){
				var data ={}
				data = {pids:pid}
				request({
					url: "/patrol2/limitRectifyPass2",
					method: "POST",
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
				
					if (res.data.success == true) {
						uni.showToast({
							title: '操作成功',
							
						})
						this.getCpatrol()
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			reject2(pid){
				var data ={}
				uni.redirectTo({
					url:"/pages/reject2/reject2?pid="+pid
				})
			},
			//一级复查通过
			review1Pass(pid){
				var data ={}
				data = {pids:pid}
				request({
					url: "/patrol2/checkLimitRectifyPass1",
					method: "POST",
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
				
					if (res.data.success == true) {
						uni.showToast({
							title: '操作成功',
							
						})
						this.getCpatrol()
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			//一级复查驳回
			review1reject(pid){
				var data ={}
				data = {pids:pid}
				var data ={}
				uni.redirectTo({
					url:"/pages/reject2/reject2?pid="+pid
				})
			},
			
			
			//二级复查通过
			review2Pass(pid){
				var data ={}
				data = {pids:pid}
				request({
					url: "/patrol2/checkLimitRectifyPass2",
					method: "POST",
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
				
					if (res.data.success == true) {
						uni.showToast({
							title: '操作成功',
							
						})
						this.getCpatrol()
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			//二级复查驳回
			review2reject(pid){
				var data ={}
				data = {pids:pid}
				request({
					url: "/patrol2/checkLimitRectifyDeny2",
					method: "POST",
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
				
					if (res.data.success == true) {
						uni.showToast({
							title: '操作成功',
							
						})
						this.getCpatrol()
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			uploadReform(pid){
				var data ={}
				data = {pids:pid}
				request({
					url: "/patrol2/handleRectify1",
					method: "POST",
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					if (res.data.success == true) {
						uni.showToast({
							title: '操作成功',
							
						})
						this.getCpatrol()
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			addcapatrol(){
				uni.redirectTo({
					url:"/pages/add_cipdetail/add_cipdetail"
				})
			},
			filter(){
				var data = {}
				if(this.pmode.toString().trim()!=''){
					data.query_pmode = this.pmode
				}
				if(this.presult.toString().trim()!=''){
					data.query_presult = this.presult
				}
				(this.cproject)
				if(this.cproject.toString().trim()!=''){
					data.query_cproject = this.cproject
				}
				this.getCpatrol(data)
			},
			//查看明细
			goCpatrolDetail(pid){
				(pid)
				uni.redirectTo({
					url:"/pages/cpatrol_detail/cpatrol_detail?pid="+pid
				})
			},
			//获取已办信息
			getCpatrol(data) {
				var that = this
				var data ={}
				data.sort = 'updatetime'
				data.order = 'desc'
				//data.query_chman=this.userInfo.user.id
				
				if(this.userInfo.dept.deptName=='市场科'){
					data.query_ptype = "01"
				}else if(this.userInfo.dept.deptName=='安全科'){
					data.query_ptype = "02"
				}else if(this.userInfo.dept.deptName=='质监科'){
					data.query_ptype = "03"
				}
				
				request({
					url: "/thirdGrid/query/c_patrol2-m_patrol",
					method: "POST",
					data: data,
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					if (res.data.success == true) {
						that.cpatrolList = res.data.rows
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			
			
			
			
			
			
			
			bindPickerChanges(e) {
				(e)
				this.index = e.target.value
				this.pmode = this.pModes[this.index].id
			},
			bindPresultChanges(e) {
				(e)
				this.presultindex = e.target.value
				this.presult = this.presults[this.presultindex].id
			},
			bindProtectChanges(e) {
				(e)
				this.cprojectIndex = e.target.value
				this.cproject = this.cprojects[this.cprojectIndex].id
			},
			chargeFilter(){
				if(this.showFilter){
					this.showFilter = false
				}else{
					this.showFilter = true
				}
			},
			getPmode() {
				var that = this
				request({
					url: "/thirdWidget/comboJson/c_cpatrol-pmode",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					if (res.data.code == '200') {
						that.pModes = []
						for (var i = 0; i < res.data.data.length; i++) {
							var mode = {}
							mode.id = res.data.data[i].id
							mode.cn = res.data.data[i].cn
							if (mode.id == '') {
								mode.id = ' '
								mode.cn = '无'
							}
							that.pModes.push(mode)
						}
						that.pmode = that.pModes[that.index].id
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			getPresults() {
				var that = this
				request({
					url: "/thirdWidget/comboJson/c_cpatrol-presult",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					if (res.data.code == '200') {
						that.presults = []
						for (var i = 0; i < res.data.data.length; i++) {
			
							var result = {}
							result.id = res.data.data[i].id
							result.cn = res.data.data[i].cn
							if (result.id == '') {
								result.id = ' '
								result.cn = '无'
							}
							that.presults.push(result)
						}
						that.presult = that.presults[that.presultindex].id
			
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
			getProjects() {
				var that = this
				request({
					url: "/thirdWidget/findJson?code=c_cpatrol&field=cproject",
					method: "POST",
					header: {
						'Content-Type': 'application/x-www-form-urlencoded',
						'cookie': uni.getStorageSync('cookie')
					}
				}).then(res => {
					if (res.data.success == true) {
						that.cprojects = [{id:' ',cn:'无'}]
						for (var i = 0; i < res.data.rows.length; i++) {
			
							var project = {}
							project.id = res.data.rows[i].cpid
							project.cn = res.data.rows[i].csname
							if (project.id == '') {
								project.id = ' '
								project.cn = '无'
							}
							that.cprojects.push(project)
						}
						that.cproject = that.cprojects[that.cprojectIndex].id
			
					} else {
						uni.showToast({
							title: res.data.msg,
							
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
@import '@/styles/variables.scss';

.done-page {
	min-height: 100vh;
	padding: $spacing-lg;
	background: linear-gradient(135deg, #52c41a 0%, #389e0d 100%);
}

.page-header {
	margin-bottom: $spacing-lg;
	
	.header-content {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: $spacing-md 0;
	}
	
	.page-title {
		font-size: $font-size-xl;
		font-weight: 600;
		color: $text-white;
		text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.3);
	}
	
	.task-count {
		font-size: $font-size-sm;
		color: rgba(255, 255, 255, 0.9);
		background: rgba(255, 255, 255, 0.2);
		padding: $spacing-xs $spacing-sm;
		border-radius: $border-radius-lg;
	}
}

.empty-state {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 120rpx $spacing-xl;
	background: $white;
	border-radius: $border-radius-lg;
	box-shadow: $shadow-md;
	
	.empty-icon {
		font-size: 120rpx;
		margin-bottom: $spacing-lg;
		opacity: 0.6;
	}
	
	.empty-title {
		font-size: $font-size-lg;
		font-weight: 600;
		color: $text-primary;
		margin-bottom: $spacing-sm;
	}
	
	.empty-desc {
		font-size: $font-size-sm;
		color: $text-secondary;
		text-align: center;
		line-height: 1.5;
	}
}

.task-list {
	display: flex;
	flex-direction: column;
	gap: $spacing-lg;
}

.task-card {
	background: $white;
	border-radius: $border-radius-lg;
	padding: $spacing-xl;
	box-shadow: $shadow-md;
	position: relative;
	transition: all 0.3s ease;
	cursor: pointer;
	border-left: 4px solid $success-color;
	
	&:active {
		transform: translateY(2rpx);
		box-shadow: $shadow-sm;
	}
}

.card-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	margin-bottom: $spacing-lg;
	gap: $spacing-md;
}

.task-time {
	display: flex;
	align-items: center;
	gap: $spacing-xs;
	
	.time-icon {
		font-size: $font-size-sm;
		color: $text-secondary;
	}
	
	.time-text {
		font-size: $font-size-sm;
		color: $text-secondary;
	}
}

.project-name {
	margin-bottom: $spacing-lg;
	
	.name-text {
		font-size: $font-size-lg;
		font-weight: 600;
		color: $text-primary;
		line-height: 1.4;
	}
}

.task-info {
	display: flex;
	flex-direction: column;
	gap: $spacing-sm;
	margin-bottom: $spacing-lg;
}

.info-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: $spacing-sm;
	background: $gray-50;
	border-radius: $border-radius-base;
	
	.info-label {
		font-size: $font-size-sm;
		color: $text-secondary;
		font-weight: 500;
		min-width: 140rpx;
	}
	
	.info-value {
		font-size: $font-size-sm;
		color: $text-primary;
		font-weight: 500;
		
		&.flow-node {
			color: $primary-color;
			font-weight: 600;
		}
	}
	
	&.score-row {
		background: lighten($success-color, 45%);
		border: 1px solid lighten($success-color, 30%);
		
		.score-value {
			color: $success-color;
			font-weight: 700;
			font-size: $font-size-base;
		}
	}
}

.completion-badge {
	position: absolute;
	top: $spacing-md;
	right: $spacing-md;
	display: flex;
	align-items: center;
	gap: $spacing-xs;
	background: $success-color;
	color: $text-white;
	padding: $spacing-xs $spacing-sm;
	border-radius: $border-radius-lg;
	font-size: $font-size-xs;
	font-weight: 600;
	box-shadow: $shadow-sm;
	
	.completion-icon {
		font-size: $font-size-sm;
		font-weight: bold;
	}
	
	.completion-text {
		font-size: $font-size-xs;
	}
}
</style>